package com.chencs.biz.special.service;

import com.chencs.biz.special.mapper.SpecialMapper;
import com.chencs.vo.biz.product.ProductModel;
import com.chencs.vo.biz.special.SpecialModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by fangzehua on 2017/7/8.
 */
@Service
@Transactional
public class SpecialService {

    @Autowired
    private SpecialMapper mapper;

   public List<SpecialModel> getSpecialList(){

       return  mapper.getSpecialList();
    }

    public void insertSpecial(SpecialModel mode){

        mapper.insertSpecial(mode);
    }

    public List<ProductModel> findSpecialProBySid(String sid){

        return mapper.findSpecialProBySid(sid);
    }

    public  void updateSpecial(SpecialModel mode){

        //先去找商品
        List<ProductModel> productModelList = mapper.findSpecialProByFid(mode.getFid());
        if(!productModelList.isEmpty()){
            mapper.delAllProductBySId(mode.getId());
            Map<String,String> map = null;
          for (int i=0;i<productModelList.size();i++){
              map = new HashMap<>();
              map.put("sid",mode.getId());
              map.put("pid",productModelList.get(i).getPid());
              mapper.insertSpecialProduct(map);
          }
        }
        mapper.updateSpecial(mode);

    }

    public void delSpecial(String id){
        mapper.delAllProductBySId(id);
        mapper.delSpecial(id);
    }

    public  SpecialModel findSpecial(String id){

        return  mapper.findSpecial(id);
    }

    public void delSpecialProduct(String id){
        String str[] = id.split("_");
        Map<String,String> map = new HashMap<>();
        map.put("sid",str[0]);
        map.put("pid",str[1]);
        mapper.delSpecialProduct(map);
    }

    public void insertSpecialProduct(String id){
        String str[] = id.split("_");
        Map<String,String> map = new HashMap<>();
        map.put("sid",str[0]);
        map.put("pid",str[1]);
        mapper.insertSpecialProduct(map);
    }
}
